package simpleTaskDispatcher.model;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;

import simpleTaskDispatcher.model.tasks.impl.ContextFreeTask;

public class TestTask extends ContextFreeTask {

	Semaphore semaphore = new Semaphore(0);
	
	String name;
	
	BlockingQueue<String> eventsLogs = new LinkedBlockingQueue<String>();
	
	public TestTask(String name,
			BlockingQueue<String> eventsLogs) {
		super();
		this.name = name;
		this.eventsLogs = eventsLogs;
	}

	public TestTask() {
		super();
	}

	@Override
	public void execute() throws InterruptedException {
		if (eventsLogs != null) {
			System.out.println("start of " + name);
			eventsLogs.put("start of " + name);
		}
		
		semaphore.acquire();
		
		if (eventsLogs != null) {
			System.out.println("end of " + name);
			eventsLogs.put("end of " + name);
		}
	}
	
	public void finished() {
		semaphore.release();
	}
	
	@Override
	public TestTask setWorkValue(int workValue) {
		return (TestTask) super.setWorkValue(workValue);
	}

}
